<?php
/**
 * diy上传图片
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/4/13
 * Time: 15:48
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\api\controller;

use app\BaseController;
use app\common\lib\ArrClass;
use app\common\lib\Show;
use app\common\lib\Status;
use think\Exception;

class Upload extends BaseController
{
    public function miniShop()
    {
        //判断上传类型
        if (!$this->request->isPost()) {
            throw new Exception("请求类型错误", 400);
        }
        $arrType = ArrClass::uploadAttr();
        try {
            $objClass = ArrClass::initClass('image', $arrType, [], true);
            $res = $objClass->upload($this->request);
        } catch (\Exception $e) {
            return Show::json_show(Status::status_error, $e->getMessage());
        }
        $result = [];
        $src = (string)url("/{$res}", [], false, true);
//        $src = "https://ywk.jixuantiant.com/".$res;
        if ($res) {
            $result = [
                "success" => true,
                "filename" => basename($res),
                "data" => [
                    "src" => $src
                ]
            ];
        }
        return json($result);
    }
}